playback function

This is a discussion on playback function within the C++ Programming forums, part of the General Programming Boards category; i need help figuring out what statement i need to put in atl ine 142 to achieve playback Code: //session ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    playback function



    i need help figuring out what
    statement i need to put in atl ine 142 to achieve playback






    Code:
    //session 3 task
    // Jcobb 29 Sept 2011
    // PMA 2011
    #include<iostream>
    #include<cctype>//Character handling library
    #include<windows.h>// WinApi header file include beep function
    using std::cout;
    using std::cin;
    using std::endl;
     
    int main()
    {
    //***********************************************************************************
    // Set things up
    //***********************************************************************************
    //notes
    constunsignedshort C = 523L; //C5 frequency in Hz
    constunsignedshort D = 587L; //D5 
    constunsignedshort E = 659L; //E5 
    constunsignedshort F = 698L; //F5 
    constunsignedshort G = 784L; //G5 
    constunsignedshort A = 880L; //A5
    constunsignedshort B = 988L; //B5 
    //durations
    //Note durations based on a fixed 120BPM tempo
    // 120BPM = 2 quater notes per second so 1 quarter note = 0.5s or 500ms
    constunsignedlong whole_note = 2000L; //semibreve
    constunsignedlong half_note = 1000L; //minim
    constunsignedlong quarter_note = 500L; //crotchet
    constunsignedlong eight_note = 250L; //quaver
    int seq_len = 0;
    char note = ' '; //Temporarily holds input notes for checking
    int duration = 0; //Temporarily holds input duration for checking
    char *note_ptr = nullptr;
    int *note_freq_ptr = nullptr;
    int *duration_ptr = nullptr; 
    char *notes_start_addr = nullptr; //Hold the start address of the notes in memory
    int *duration_start_addr = nullptr;
    int *freq_start_addr = nullptr;
    //***********************************************************************************
    // Get the user to define the length of the sequencer in notes
    //***********************************************************************************
    cout << "Enter a value between 4 and 20 to set the sequence length" << endl;
    cin >> seq_len;
    if ( (seq_len < 4 ) || (seq_len > 20) )
    {
    cout << "Invalid sequence length forces exit " << endl;
    exit(0);
    }
     
    //***********************************************************************************
    // Now we know the sequence length dynamically allocate the memory to hold the notes
    // and their durations.
    //*********************************************************************************** 
    note_ptr = newchar[seq_len]; //allocate some memory depending on sequence length
    note_freq_ptr = newint[seq_len]; //To hold notes converted to frequencies
    duration_ptr = newint[seq_len];
    notes_start_addr = note_ptr;//store start position of memory allocated to hold notes
    duration_start_addr = duration_ptr;
    freq_start_addr = note_freq_ptr;
    //*********************************************************************************** 
    // get the user to enter the notes from a,b,c,d,e,f,g and their durations 
    // 2000 = whole note, 1000 = half note, 500 = quater note, 250 = eighth note
    // store this user data in the dynamically allocated char arrays
    //***********************************************************************************
    for(int i=0; i <= seq_len-1; i++)
    {
    cout << "Enter the note name for note number: " << i << " : ";
    cin >> note;
    *note_ptr = note; //The input values is stored in the address pointed at by note_ptr
    note_ptr++; //Point to next location ready for next note
    cout << "Enter the note duration for note number: " << i << " : ";
    cin >> duration;
    *duration_ptr = duration;
    duration_ptr++;
    cout << endl;
    }
    //*********************************************************************************** 
    //*********************************************************************************** 
    // Convert the char type note names entered by the user to integer frequencies
    //*********************************************************************************** 
    for(int i=0; i <= seq_len-1; i++)
    {
    if ( *(notes_start_addr+i) == 'a') { *(note_freq_ptr+i) = A; }
    elseif ( *(notes_start_addr+i) == 'b') { *(note_freq_ptr+i) = B; }
    elseif ( *(notes_start_addr+i) == 'c') { *(note_freq_ptr+i) = C; }
    elseif ( *(notes_start_addr+i) == 'd') { *(note_freq_ptr+i) = D; }
    elseif ( *(notes_start_addr+i) == 'e') { *(note_freq_ptr+i) = E; }
    elseif ( *(notes_start_addr+i) == 'f') { *(note_freq_ptr+i) = F; }
    elseif ( *(notes_start_addr+i) == 'g') { *(note_freq_ptr+i) = G; }
    }
    //***********************************************************************************
    //***********************************************************************************
     
     
     
     
     
     
     
    // Print out the notes and durations loaded into the sequencer
    //***********************************************************************************
    for(int i=0; i <= seq_len-1; i++)
    {
    cout << "Sequencer position: " << i << " Note= " << *(notes_start_addr+i);
    cout << " with frequency: " << *(freq_start_addr+i) << "Hz";
    cout << " and duration= " << *(duration_start_addr + i) << "ms" << endl; 
    }
    //***********************************************************************************
    //***********************************************************************************
    // Play the sequence using the Beep function
    //***********************************************************************************
    for(int i=0; i <= seq_len-1; i++)
    {
    //**Add your playback statement here**
    }
    //***********************************************************************************
    //***********************************************************************************
    // Tidy things up like release any memory to the restore and eliminate dangling pointers
    //***********************************************************************************
    note_ptr = nullptr;//Do this first otherwise assertion error as not at head of block
    note_freq_ptr = nullptr;
    duration_ptr = nullptr;
    notes_start_addr = nullptr;
    duration_start_addr = nullptr;
    delete [] note_ptr;
    delete [] note_freq_ptr;
    delete [] duration_ptr;
     
    return 0;
    }
     


  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    line 142 is where it says //**Add your playback statement here**

  3. #3
    Registered User slartie's Avatar
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    6
    While I'm still new at C++, two things strike me:

    Code:
    constunsignedshort C = 523L; //C5 frequency in Hz
    That doesn't make a whole lot of sense.

    I'm guessing it should be:

    Code:
        const unsigned short C = 523L; //C5 frequency in Hz
        const unsigned short D = 587L; //D5
        const unsigned short E = 659L; //E5
        const unsigned short F = 698L; //F5
        const unsigned short G = 784L; //G5
        const unsigned short A = 880L; //A5
        const unsigned short B = 988L; //B5
    
    
        //durations
        //Note durations based on a fixed 120BPM tempo
        // 120BPM = 2 quater notes per second so 1 quarter note = 0.5s or 500ms
        const unsigned long whole_note = 2000L;     //semibreve
        const unsigned long half_note = 1000L;      //minim
        const unsigned long quarter_note = 500L;    //crotchet
        const unsigned long eight_note = 250L;      //quaver
    Second:

    Code:
        char *note_ptr = nullptr;
        int *note_freq_ptr = nullptr;
        int *duration_ptr = nullptr;
        char *notes_start_addr = nullptr; //Hold the start address of the notes in memory
        int *duration_start_addr = nullptr;
        int *freq_start_addr = nullptr;
    nullptr isn't declared anywhere, and I'm not sure you can mix types.

    That's as far as I got. I'm still trying to figure out how pointers work.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by slarlie
    nullptr isn't declared anywhere, and I'm not sure you can mix types.
    Actually, nullptr is a keyword and denotes a null pointer literal of *any* type.

    btw... congratulations for being able to read such obfuscated code!
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User slartie's Avatar
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    6
    Quote Originally Posted by manasij7479 View Post
    Actually, nullptr is a keyword and denotes a null pointer literal of *any* type.

    btw... congratulations for being able to read such obfuscated code!
    Ah, I learned something new then. Bonus!

    To be quite honest, I find that I learn more from these messed up homework assignments people are throwing in here, than anything I've read in the books so far.

    However, I can't help but wonder why these students aren't even spending a few minutes reading, experimenting and tweaking a little bit rather than just saying: "this doesn't work - fix it!"

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by slartie View Post
    However, I can't help but wonder why these students aren't even spending a few minutes reading, experimenting and tweaking a
    little bit rather than just saying: "this doesn't work - fix it!"
    Well, that is somewhat better than "write the stupid thingy for me" or "gimme teh codes" , only when the code isn't copy-paste code.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User slartie's Avatar
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    6
    Found some more gibberish

    Code:
    note_ptr = newchar[seq_len]; //allocate some memory depending on sequence length
    note_freq_ptr = newint[seq_len]; //To hold notes converted to frequencies
    duration_ptr = newint[seq_len];
    should probably be ...

    Code:
        note_ptr = new char[seq_len];        //allocate some memory depending on sequence length
        note_freq_ptr = new int[seq_len];    //To hold notes converted to frequencies
        duration_ptr = new int[seq_len];
    Code:
    for(int i=0; i <= seq_len-1; i++)
    {
    if ( *(notes_start_addr+i) == 'a') { *(note_freq_ptr+i) = A; }
    elseif ( *(notes_start_addr+i) == 'b') { *(note_freq_ptr+i) = B; }
    elseif ( *(notes_start_addr+i) == 'c') { *(note_freq_ptr+i) = C; }
    elseif ( *(notes_start_addr+i) == 'd') { *(note_freq_ptr+i) = D; }
    elseif ( *(notes_start_addr+i) == 'e') { *(note_freq_ptr+i) = E; }
    elseif ( *(notes_start_addr+i) == 'f') { *(note_freq_ptr+i) = F; }
    elseif ( *(notes_start_addr+i) == 'g') { *(note_freq_ptr+i) = G; }
    }
    should probably be ...

    Code:
        for(int i=0; i <= seq_len-1; i++) {
            if ( *(notes_start_addr + i) == 'a') { *(note_freq_ptr + i) = A; }
            else if ( *(notes_start_addr + i) == 'b') { *(note_freq_ptr + i) = B; }
            else if ( *(notes_start_addr + i) == 'c') { *(note_freq_ptr + i) = C; }
            else if ( *(notes_start_addr + i) == 'd') { *(note_freq_ptr + i) = D; }
            else if ( *(notes_start_addr + i) == 'e') { *(note_freq_ptr + i) = E; }
            else if ( *(notes_start_addr + i) == 'f') { *(note_freq_ptr + i) = F; }
            else if ( *(notes_start_addr + i) == 'g') { *(note_freq_ptr + i) = G; }
        }
    Yes, I've been changing the braces and putting in whitespace for easier reading as well.

    Still not helping with your current problem, but how about you tell what you've tried so far?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone else have stuttering playback with the new flash?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-21-2008, 02:52 PM
  2. PlaySound repetitive playback
    By Gerread in forum Windows Programming
    Replies: 3
    Last Post: 11-22-2007, 07:08 PM
  3. Playback microphone through DBS
    By hdragon in forum Tech Board
    Replies: 0
    Last Post: 06-11-2007, 02:38 PM
  4. [Win32] MP3 Playback
    By Lionel in forum Windows Programming
    Replies: 9
    Last Post: 01-02-2006, 03:55 PM
  5. DVD playback error [Media Player 9]
    By psychopath in forum Tech Board
    Replies: 2
    Last Post: 08-06-2005, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21