annotate src/cdrom/macosx/AudioFileReaderThread.cpp @ 773:da0a2ad35bf4

Date: Sun, 4 Jan 2004 23:48:19 +0100 From: Max Horn Subject: Re: Again Audio CD patch Am 04.01.2004 um 22:38 schrieb Sam Lantinga: > > Okay, I fixed the buffering problems by simply using a 4 second buffer > instead of a 1 second buffer. However, using your code I can't play an > entire CD - the playback stops after the first song. > Found the problem: FSReadFork returns eofErr when the file is finished. However, we check its return value for errors, and if anything but noErr occurs, the reader thread aborts its current iteration. That is bad, because it aborts before it can ever set the flag which tells that the file is over (also, any remaining data which FSRead did return is lost - so you'd not hear about to 4 seconds from the end of the file. Furthermore, the computed data size was 8 bytes to high (I forgot to account for the fact that the size of an (A)IFF chunk always contains the chunk header & size fields, too). This is enough to make it work. However, the end condition is rather fragile, so I tuned some other things to be pessimistic (check for <= 0 instead of == 0, when eofErr is encountered enforce mReadFilePosition == mFileLength). You never know... The attached patch fixes the issue for me.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 05 Jan 2004 00:57:51 +0000
parents de1b2c3063b9
children 08b7fc2b5225
rev   line source
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /*
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6 modify it under the terms of the GNU Library General Public
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8 version 2 of the License, or (at your option) any later version.
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 Library General Public License for more details.
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 You should have received a copy of the GNU Library General Public
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 License along with this library; if not, write to the Free
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 Sam Lantinga
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 slouken@libsdl.org
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 This file based on Apple sample code. We haven't changed the file name,
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23 so if you want to see the original search for it on apple.com/developer
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 */
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
27 // AudioFileManager.cpp
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28 //
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 #include "AudioFilePlayer.h"
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30 #include <mach/mach.h> //used for setting policy of thread
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31 #include "CAGuard.h"
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 #include <pthread.h>
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34 #include <list>
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 class FileReaderThread {
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37 public:
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
38 FileReaderThread ();
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
39
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
40 CAGuard& GetGuard() { return mGuard; }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
41
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
42 void AddReader();
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
43
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
44 void RemoveReader (AudioFileManager* inItem);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
45
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
46 // returns true if succeeded
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
47 bool TryNextRead (AudioFileManager* inItem)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
48 {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
49 bool didLock = false;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
50 bool succeeded = false;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
51 if (mGuard.Try (didLock))
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
52 {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
53 mFileData.push_back (inItem);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
54 mGuard.Notify();
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
55 succeeded = true;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
56
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
57 if (didLock)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
58 mGuard.Unlock();
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
59 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
60
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
61 return succeeded;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
62 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
63
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
64 int mThreadShouldDie;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66 private:
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
67 typedef std::list<AudioFileManager*> FileData;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
69 CAGuard mGuard;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
70 UInt32 mThreadPriority;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
71
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
72 int mNumReaders;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
73 FileData mFileData;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
76 void ReadNextChunk ();
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
78 void StartFixedPriorityThread ();
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
79 static UInt32 GetThreadBasePriority (pthread_t inThread);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
80
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
81 static void* DiskReaderEntry (void *inRefCon);
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
82 };
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 FileReaderThread::FileReaderThread ()
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
85 : mThreadPriority (62),
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
86 mNumReaders (0)
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
87 {
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
90 void FileReaderThread::AddReader()
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91 {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
92 if (mNumReaders == 0)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
93 {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
94 mThreadShouldDie = false;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
95
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
96 StartFixedPriorityThread ();
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
97 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
98 mNumReaders++;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
99 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
101 void FileReaderThread::RemoveReader (AudioFileManager* inItem)
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
102 {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
103 if (mNumReaders > 0)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
104 {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
105 CAGuard::Locker fileReadLock (mGuard);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
106
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
107 mFileData.remove (inItem);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
108
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
109 if (--mNumReaders == 0) {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
110 mThreadShouldDie = true;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
111 mGuard.Notify(); // wake up thread so it will quit
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
112 mGuard.Wait(); // wait for thread to die
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
113 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
114 }
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
116
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
117 void FileReaderThread::StartFixedPriorityThread ()
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
118 {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
119 pthread_attr_t theThreadAttrs;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
120 pthread_t pThread;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
121
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
122 OSStatus result = pthread_attr_init(&theThreadAttrs);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
123 THROW_RESULT("pthread_attr_init - Thread attributes could not be created.")
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
124
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
125 result = pthread_attr_setdetachstate(&theThreadAttrs, PTHREAD_CREATE_DETACHED);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
126 THROW_RESULT("pthread_attr_setdetachstate - Thread attributes could not be detached.")
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
127
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
128 result = pthread_create (&pThread, &theThreadAttrs, DiskReaderEntry, this);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
129 THROW_RESULT("pthread_create - Create and start the thread.")
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
130
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
131 pthread_attr_destroy(&theThreadAttrs);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
132
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
133 // we've now created the thread and started it
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
134 // we'll now set the priority of the thread to the nominated priority
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
135 // and we'll also make the thread fixed
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
136 thread_extended_policy_data_t theFixedPolicy;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
137 thread_precedence_policy_data_t thePrecedencePolicy;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
138 SInt32 relativePriority;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
139
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
140 // make thread fixed
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
141 theFixedPolicy.timeshare = false; // set to true for a non-fixed thread
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
142 result = thread_policy_set (pthread_mach_thread_np(pThread), THREAD_EXTENDED_POLICY, (thread_policy_t)&theFixedPolicy, THREAD_EXTENDED_POLICY_COUNT);
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
143 THROW_RESULT("thread_policy - Couldn't set thread as fixed priority.")
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
144 // set priority
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
145 // precedency policy's "importance" value is relative to spawning thread's priority
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
146 relativePriority = mThreadPriority - FileReaderThread::GetThreadBasePriority (pthread_self());
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
147
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
148 thePrecedencePolicy.importance = relativePriority;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
149 result = thread_policy_set (pthread_mach_thread_np(pThread), THREAD_PRECEDENCE_POLICY, (thread_policy_t)&thePrecedencePolicy, THREAD_PRECEDENCE_POLICY_COUNT);
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
150 THROW_RESULT("thread_policy - Couldn't set thread priority.")
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
151 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
152
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
153 UInt32 FileReaderThread::GetThreadBasePriority (pthread_t inThread)
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
154 {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
155 thread_basic_info_data_t threadInfo;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
156 policy_info_data_t thePolicyInfo;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
157 unsigned int count;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
158
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
159 // get basic info
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
160 count = THREAD_BASIC_INFO_COUNT;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
161 thread_info (pthread_mach_thread_np (inThread), THREAD_BASIC_INFO, (integer_t*)&threadInfo, &count);
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
162
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
163 switch (threadInfo.policy) {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
164 case POLICY_TIMESHARE:
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
165 count = POLICY_TIMESHARE_INFO_COUNT;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
166 thread_info(pthread_mach_thread_np (inThread), THREAD_SCHED_TIMESHARE_INFO, (integer_t*)&(thePolicyInfo.ts), &count);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
167 return thePolicyInfo.ts.base_priority;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
168 break;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
169
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
170 case POLICY_FIFO:
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
171 count = POLICY_FIFO_INFO_COUNT;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
172 thread_info(pthread_mach_thread_np (inThread), THREAD_SCHED_FIFO_INFO, (integer_t*)&(thePolicyInfo.fifo), &count);
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
173 if (thePolicyInfo.fifo.depressed) {
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
174 return thePolicyInfo.fifo.depress_priority;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
175 } else {
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
176 return thePolicyInfo.fifo.base_priority;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
177 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
178 break;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
179
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
180 case POLICY_RR:
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
181 count = POLICY_RR_INFO_COUNT;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
182 thread_info(pthread_mach_thread_np (inThread), THREAD_SCHED_RR_INFO, (integer_t*)&(thePolicyInfo.rr), &count);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
183 if (thePolicyInfo.rr.depressed) {
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
184 return thePolicyInfo.rr.depress_priority;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
185 } else {
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
186 return thePolicyInfo.rr.base_priority;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
187 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
188 break;
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
189 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
190
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
191 return 0;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
192 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
193
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
194 void *FileReaderThread::DiskReaderEntry (void *inRefCon)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
195 {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
196 FileReaderThread *This = (FileReaderThread *)inRefCon;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
197 This->ReadNextChunk();
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
198 #if DEBUG
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
199 printf ("finished with reading file\n");
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
200 #endif
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
201
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
202 return 0;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
203 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
204
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
205 void FileReaderThread::ReadNextChunk ()
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
206 {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
207 OSStatus result;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
208 UInt32 dataChunkSize;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
209 AudioFileManager* theItem = 0;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
210
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
211 for (;;)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
212 {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
213 { // this is a scoped based lock
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
214 CAGuard::Locker fileReadLock (mGuard);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
215
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
216 if (this->mThreadShouldDie) {
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
217
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
218 mGuard.Notify();
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
219 return;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
220 }
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
221
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
222 if (mFileData.empty())
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
223 {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
224 mGuard.Wait();
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
225 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
226
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
227 // kill thread
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
228 if (this->mThreadShouldDie) {
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
229
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
230 mGuard.Notify();
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
231 return;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
232 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
233
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
234 theItem = mFileData.front();
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
235 mFileData.pop_front();
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
236 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
237
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
238 if ((theItem->mFileLength - theItem->mReadFilePosition) < theItem->mChunkSize)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
239 dataChunkSize = theItem->mFileLength - theItem->mReadFilePosition;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
240 else
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
241 dataChunkSize = theItem->mChunkSize;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
242
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
243 // this is the exit condition for the thread
773
da0a2ad35bf4 Date: Sun, 4 Jan 2004 23:48:19 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 768
diff changeset
244 if (dataChunkSize <= 0) {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
245 theItem->mFinishedReadingData = true;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
246 continue;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
247 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
248 // construct pointer
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
249 char* writePtr = const_cast<char*>(theItem->GetFileBuffer() +
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
250 (theItem->mWriteToFirstBuffer ? 0 : theItem->mChunkSize));
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
251
773
da0a2ad35bf4 Date: Sun, 4 Jan 2004 23:48:19 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 768
diff changeset
252 // read data
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
253 result = theItem->Read(writePtr, &dataChunkSize);
773
da0a2ad35bf4 Date: Sun, 4 Jan 2004 23:48:19 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 768
diff changeset
254 if (result != noErr && result != eofErr) {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
255 theItem->GetParent().DoNotification(result);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
256 continue;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
257 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
258
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
259 if (dataChunkSize != theItem->mChunkSize)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
260 {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
261 writePtr += dataChunkSize;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
262
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
263 // can't exit yet.. we still have to pass the partial buffer back
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
264 memset (writePtr, 0, (theItem->mChunkSize - dataChunkSize));
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
265 }
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
266
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
267 theItem->mWriteToFirstBuffer = !theItem->mWriteToFirstBuffer; // switch buffers
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
268
773
da0a2ad35bf4 Date: Sun, 4 Jan 2004 23:48:19 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 768
diff changeset
269 if (result == eofErr)
da0a2ad35bf4 Date: Sun, 4 Jan 2004 23:48:19 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 768
diff changeset
270 theItem->mReadFilePosition = theItem->mFileLength;
da0a2ad35bf4 Date: Sun, 4 Jan 2004 23:48:19 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 768
diff changeset
271 else
da0a2ad35bf4 Date: Sun, 4 Jan 2004 23:48:19 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 768
diff changeset
272 theItem->mReadFilePosition += dataChunkSize; // increment count
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
273 }
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
274 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
275
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
276
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
277 static FileReaderThread sReaderThread;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
278
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
279 AudioFileManager::AudioFileManager (AudioFilePlayer &inParent,
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
280 SInt16 inForkRefNum,
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
281 SInt64 inFileLength,
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
282 UInt32 inChunkSize)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
283 : mParent (inParent),
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
284 mForkRefNum (inForkRefNum),
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
285 mFileBuffer (0),
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
286 mByteCounter (0),
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
287 mLockUnsuccessful (false),
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
288 mIsEngaged (false),
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
289
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
290 mChunkSize (inChunkSize),
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
291 mFileLength (inFileLength),
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
292 mReadFilePosition (0),
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
293 mWriteToFirstBuffer (false),
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
294 mFinishedReadingData (false)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
295
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
296 {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
297 mFileBuffer = (char*) malloc (mChunkSize * 2);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
298 FSGetForkPosition(mForkRefNum, &mAudioDataOffset);
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
299 assert (mFileBuffer != NULL);
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
300 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
301
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
302 void AudioFileManager::DoConnect ()
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
303 {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
304 if (!mIsEngaged)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
305 {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
306 //mReadFilePosition = 0;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
307 mFinishedReadingData = false;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
308
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
309 mNumTimesAskedSinceFinished = -1;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
310 mLockUnsuccessful = false;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
311
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
312 OSStatus result;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
313 UInt32 dataChunkSize;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
314
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
315 if ((mFileLength - mReadFilePosition) < mChunkSize)
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
316 dataChunkSize = mFileLength - mReadFilePosition;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
317 else
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
318 dataChunkSize = mChunkSize;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
319
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
320 result = Read(mFileBuffer, &dataChunkSize);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
321 THROW_RESULT("AudioFileManager::DoConnect(): Read")
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
322
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
323 mReadFilePosition += dataChunkSize;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
324
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
325 mWriteToFirstBuffer = false;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
326 mReadFromFirstBuffer = true;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
327
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
328 sReaderThread.AddReader();
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
329
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
330 mIsEngaged = true;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
331 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
332 else
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
333 throw static_cast<OSStatus>(-1); //thread has already been started
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
334 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
335
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
336 void AudioFileManager::Disconnect ()
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
337 {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
338 if (mIsEngaged)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
339 {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
340 sReaderThread.RemoveReader (this);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
341 mIsEngaged = false;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
342 }
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
343 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
344
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
345 OSStatus AudioFileManager::Read(char *buffer, UInt32 *len)
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
346 {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
347 return FSReadFork (mForkRefNum,
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
348 fsFromStart,
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
349 mReadFilePosition + mAudioDataOffset,
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
350 *len,
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
351 buffer,
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
352 len);
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
353 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
354
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
355 OSStatus AudioFileManager::GetFileData (void** inOutData, UInt32 *inOutDataSize)
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
356 {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
357 if (mFinishedReadingData)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
358 {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
359 ++mNumTimesAskedSinceFinished;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
360 *inOutDataSize = 0;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
361 *inOutData = 0;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
362 return noErr;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
363 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
364
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
365 if (mReadFromFirstBuffer == mWriteToFirstBuffer) {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
366 #if DEBUG
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
367 printf ("* * * * * * * Can't keep up with reading file:%ld\n", mParent.GetBusNumber());
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
368 #endif
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
369
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
370 mParent.DoNotification (kAudioFilePlayErr_FilePlayUnderrun);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
371 *inOutDataSize = 0;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
372 *inOutData = 0;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
373 } else {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
374 *inOutDataSize = mChunkSize;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
375 *inOutData = mReadFromFirstBuffer ? mFileBuffer : (mFileBuffer + mChunkSize);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
376 }
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
377
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
378 mLockUnsuccessful = !sReaderThread.TryNextRead (this);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
379
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
380 mReadFromFirstBuffer = !mReadFromFirstBuffer;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
381
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
382 return noErr;
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
383 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
384
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
385 void AudioFileManager::AfterRender ()
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
386 {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
387 if (mNumTimesAskedSinceFinished > 0)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
388 {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
389 bool didLock = false;
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
390 if (sReaderThread.GetGuard().Try (didLock)) {
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
391 mParent.DoNotification (kAudioFilePlay_FileIsFinished);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
392 if (didLock)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
393 sReaderThread.GetGuard().Unlock();
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
394 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
395 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
396
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
397 if (mLockUnsuccessful)
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
398 mLockUnsuccessful = !sReaderThread.TryNextRead (this);
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
399 }
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
400
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
401 void AudioFileManager::SetPosition (SInt64 pos)
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
402 {
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
403 if (pos < 0 || pos >= mFileLength) {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
404 SDL_SetError ("AudioFileManager::SetPosition - position invalid: %d filelen=%d\n",
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
405 (unsigned int)pos, (unsigned int)mFileLength);
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
406 pos = 0;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
407 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
408
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
409 mReadFilePosition = pos;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
410 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
411
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
412 void AudioFileManager::SetEndOfFile (SInt64 pos)
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
413 {
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
414 if (pos <= 0 || pos > mFileLength) {
768
de1b2c3063b9 Max has been reworking this code so it works on MacOS X 10.1
Sam Lantinga <slouken@libsdl.org>
parents: 613
diff changeset
415 SDL_SetError ("AudioFileManager::SetEndOfFile - position beyond actual eof\n");
613
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
416 pos = mFileLength;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
417 }
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
418
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
419 mFileLength = pos;
9c6717a1c66f Added MacOS X CD-ROM audio support (thanks Max and Darrell)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
420 }